<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="科室" prop="depId">
        <el-select v-model="queryParams.depId" placeholder="请选择科室">
          <el-option v-for="item in depList" :key="item.deptId" :label="item.deptName" :value="item.deptId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排班日期" prop="scheduleDate">
        <el-date-picker clearable v-model="queryParams.scheduleDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择排班日期">
        </el-date-picker>
      </el-form-item>&nbsp;&nbsp;&nbsp;&nbsp;
      <el-form-item label="排班时间" prop="scheduleTime">
        <el-select v-model="queryParams.scheduleTime" placeholder="请选择上午，下午" clearable>
          <el-option v-for="dict in dict.type.morning_afternoon" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form><br>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['reghos-serv:schedule:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['reghos-serv:schedule:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['reghos-serv:schedule:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['reghos-serv:schedule:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="scheduleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="排班id" align="center" prop="scheduleId" v-if="false" />
      <el-table-column label="医生id" align="center" prop="userId" v-if="false" />
      <el-table-column label="科室id" align="center" prop="depId" v-if="false" />
      <el-table-column label="科室名称" align="center" prop="depName" />
      <el-table-column label="排班日期" align="center" prop="scheduleDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.scheduleDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排班时间" align="center" prop="scheduleTime">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.morning_afternoon" :value="scope.row.scheduleTime" />
        </template>
      </el-table-column>
      <el-table-column label="医生姓名" align="center" prop="userName" />
      <el-table-column label="医生头像" align="center" prop="avatar" />
      <el-table-column label="接诊人数" align="center" prop="scheduleNum" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['reghos-serv:schedule:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['reghos-serv:schedule:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改排班对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="排班日期" prop="scheduleDate">
          <el-date-picker clearable v-model="form.scheduleDate" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择排班日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="排班时间" prop="scheduleTime">
          <el-select v-model="form.scheduleTime" placeholder="请选择上午，下午">
            <el-option v-for="dict in dict.type.morning_afternoon" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="科室" prop="depId">
          <el-select v-model="form.depId" placeholder="请选择科室" @change="queryUserList()">
            <el-option v-for="item in depList" :key="item.deptId" :label="item.deptName" :value="item.deptId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="医生" prop="empId">
          <el-select v-model="form.userId" placeholder="请选择医生">
            <el-option v-for="item in userList" :key="item.userId" :label="item.userName" :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="接诊人数" prop="scheduleNum">
          <el-input v-model="form.scheduleNum" placeholder="请输入接诊人数" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getUserList, listSchedule, getSchedule, delSchedule, addSchedule, updateSchedule } from "@/api/reghos-serv/schedule";
import { listDept } from "@/api/system/dept";

export default {
  name: "Schedule",
  dicts: ['morning_afternoon'],
  data() {
    return {
      aa: {
        userId: 1,
        userName: "哈哈"
      },
      haha: "",
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 排班表格数据
      scheduleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        depId: undefined,
        scheduleDate: undefined,
        scheduleTime: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        depId: [
          { required: true, message: "科室id不能为空", trigger: "change" }
        ],
        scheduleDate: [
          { required: true, message: "排班日期不能为空", trigger: "blur" }
        ],
        scheduleTime: [
          { required: true, message: "排班时间不能为空", trigger: "change" }
        ],
      },
      userList: [],
      depList: [],
    };
  },
  created() {
    this.getList();
    this.queryDepList();
  },
  methods: {
    /** 查询排班列表 */
    getList() {
      this.loading = true;
      listSchedule(this.queryParams).then(response => {
        this.scheduleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: undefined,
        depId: undefined,
        scheduleDate: undefined,
        scheduleTime: undefined,
        scheduleNum: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.scheduleId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加排班";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const scheduleId = row.scheduleId || this.ids
      getSchedule(scheduleId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改排班";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.scheduleId != null) {
            updateSchedule(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addSchedule(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const scheduleIds = row.scheduleId || this.ids;
      this.$modal.confirm('是否确认删除排班编号为"' + scheduleIds + '"的数据项？').then(() => {
        this.loading = true;
        return delSchedule(scheduleIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('reghos-serv/schedule/export', {
        ...this.queryParams
      }, `schedule_${new Date().getTime()}.xlsx`)
    },
    queryUserList() {
      getUserList(this.form.depId).then((result) => {
        this.userList = result.data.userList;
      });
    },
    queryDepList() {
      listDept().then((result) => {
        this.depList = result.data;
      });
    }
  }
};
</script>
